<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{ category.top.name }}</XtxBreadItem>
    <transition name="fade-right" mode="out-in">
      <XtxBreadItem :key="category.sub.id" v-if="category.sub">{{ category.sub.name }}</XtxBreadItem>
    </transition>

  </XtxBread>
</template>

<script setup>
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
const store = useStore()
const route = useRoute()
// eslint-disable-next-line no-unused-vars
const category = computed(() => {
  const cate = {}
  store.state.category.list.forEach(item => {
    if (item.children) { // 这里要有值才可以不然会报错
      const sub = item.children.find(i => i.id === route.params.id)
      if (sub) { // 通过此判断上一级
        cate.top = { id: item.id, name: item.name }
        cate.sub = { id: sub.id, name: sub.name }
      }
    }
  })
  return cate
})

</script>

<style scoped lang='less'>

</style>
